package kino.command;

import kino.client.KinoClient;
import kino.common.KinoConstant;
import kino.database.KinoDB;
import kino.object.KinoObject;
import kino.object.KinoObjectTypeEnum;

import java.util.HashMap;
import java.util.Map;

@KinoCommandEnable
public class HsetCommand extends AbstractKinoCommand{
    @Override
    public String doExecute(KinoClient client, String... argv) {
        KinoDB kinoDB = client.getDb();
        Map<String, KinoObject> dict = kinoDB.getDict();
        Map<String, String> hash;
        if (dict.containsKey(argv[0]) && dict.get(argv[0]).getType() == KinoObjectTypeEnum.KINO_HASH) {
            hash = (HashMap) dict.get(argv[0]).getObj();
        } else {
            KinoObject kinoObject = new KinoObject(KinoObjectTypeEnum.KINO_HASH);
            hash = (HashMap)kinoObject.getObj();
            dict.put(argv[0], kinoObject);
        }
        hash.put(argv[1], argv[2]);
        return KinoConstant.OK;
    }

    @Override
    public Integer getArgc() {
        return 3;
    }

    @Override
    public String getCommandName() {
        return "hset";
    }

    @Override
    public boolean aofFlag() {
        return true;
    }

    @Override
    public boolean hasExtParam() {
        return false;
    }
}
